package com.dkts.oil.enumes;

import com.dkts.oil.util.gis.GpsFormat;
import com.dkts.oil.util.gis.GpsPoint;
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
 * 经纬度类型
 *
 * @author heguanglong
 * @date 2020-12-02
 */
public enum GpsTypeEnums {


    /**
     * WGS84
     */
    COORDINATE_SYSTEM_WGS84(1, "WGS84"),
    /**
     * GCJ02
     */
    COORDINATE_SYSTEM_GCJ02(2, "GCJ02"),
    /**
     * BD0911
     */
    COORDINATE_SYSTEM_BD0911(3, "BD09");

    @Getter
    private Integer id;
    @Getter
    private String name;


    GpsTypeEnums(Integer id, String name) {
        this.name = name;
        this.id = id;
    }

    public static String getCodeById(Integer type) {
        for (GpsTypeEnums failReason : values()) {
            if (failReason.getId().equals(type)) {
                return failReason.name;
            }
        }
        return null;
    }

    public static GpsPoint convertGps(Double lon, Double lat, Integer type) {
        String code = getCodeById(type);
        if(org.apache.commons.lang3.StringUtils.isBlank(code)){
            return new GpsPoint(lon, lat);
        }
        if (COORDINATE_SYSTEM_BD0911.getName().equals(code)) {
            return GpsFormat.bd09ToGcj02( lon,lat);
        } else if (COORDINATE_SYSTEM_WGS84.getName().equals(code)) {
            return GpsFormat.wgs84ToGcj02(lon, lat);
        }
        return new GpsPoint(lon, lat);
    }

    public static String convertGps(String gps, Integer type) {
        String[] currentCoordinatesArray = gps.split(",");
        double lon = Double.valueOf(currentCoordinatesArray[0]);
        double lat = Double.valueOf(currentCoordinatesArray[1]);
        GpsPoint gpsPoint= null;
        
        //如果百度坐标 GCj02 转 百度坐标
        if (COORDINATE_SYSTEM_BD0911.getName().equals(type)) {
            gpsPoint =  GpsFormat.gcj02Bd09( lon,lat);
        //如果WGs84 GCJ02 转 WGs84
        } else if (COORDINATE_SYSTEM_WGS84.getName().equals(type)) {
            gpsPoint = GpsFormat.gcj02ToWgs84( lon, lat);
        }else {
            gpsPoint = new GpsPoint(lon, lat);
        }
        return gpsPoint.getLng() + "," + gpsPoint.getLat();
    }


    public static String gpsStrToGcJo2(String coordinates, Integer type) {
        String code = getCodeById(type);
        //为空或默认值不转变
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(coordinates) || GpsTypeEnums.COORDINATE_SYSTEM_GCJ02.getId().equals(type)) {
            return coordinates;
        }
        String[] coordinatesArray = coordinates.split(";");
        StringBuilder stringBuilderTrack = new StringBuilder();
        for (String currentCoordinates : coordinatesArray) {
            String[] currentCoordinatesArray = currentCoordinates.split(",");
            double lat = Double.valueOf(currentCoordinatesArray[0]);
            double lon = Double.valueOf(currentCoordinatesArray[1]);
            if (GpsTypeEnums.COORDINATE_SYSTEM_BD0911.getId().equals(type)) {
                GpsPoint gpsPoint = GpsFormat.bd09ToGcj02(lat, lon);
                stringBuilderTrack.append(gpsPoint.getLng()).append(",").append(gpsPoint.getLat()).append(";");
            } else if (GpsTypeEnums.COORDINATE_SYSTEM_WGS84.getId().equals(type)) {
                GpsPoint gpsPoint = GpsFormat.wgs84ToGcj02(lat, lon);
                stringBuilderTrack.append(gpsPoint.getLng()).append(",").append(gpsPoint.getLat()).append(";");
            }
        }
        stringBuilderTrack.deleteCharAt(stringBuilderTrack.length() - 1);
        return stringBuilderTrack.toString();
    }


    /**
     * @param coordinates
     * @param type
     * @return
     */
    public static String morePolygon(String coordinates, Integer type) {
        String code = getCodeById(type);
        //为空或默认值不转变
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(coordinates)) {
            return coordinates;
        }
        String[] coordinatesArray = coordinates.split(";");
        StringBuilder stringBuilderTrack = new StringBuilder();
        double[] coordinateArrayTemp = null;
        for (int i = 0; i < coordinatesArray.length; i++) {
            String[] currentCoordinatesArray = coordinatesArray[i].split(",");
            double lat = Double.valueOf(currentCoordinatesArray[0]);
            double lon = Double.valueOf(currentCoordinatesArray[1]);
            if (GpsTypeEnums.COORDINATE_SYSTEM_BD0911.getId().equals(type)) {
                GpsPoint gpsPoint = GpsFormat.bd09ToGcj02(lat, lon);
                if(i==0) {
                    coordinateArrayTemp=new double[2];
                    coordinateArrayTemp[0] = gpsPoint.getLng();
                    coordinateArrayTemp[1] = gpsPoint.getLat();
                }
                stringBuilderTrack.append(gpsPoint.getLng()).append(",").append(gpsPoint.getLat()).append(";");
            } else if (GpsTypeEnums.COORDINATE_SYSTEM_WGS84.getId().equals(type)) {
                GpsPoint gpsPoint = GpsFormat.wgs84ToGcj02(lat, lon);
                if(i==0) {
                    coordinateArrayTemp=new double[2];
                    coordinateArrayTemp[0] = gpsPoint.getLng();
                    coordinateArrayTemp[1] = gpsPoint.getLat();
                }
                stringBuilderTrack.append(gpsPoint.getLng()).append(",").append(gpsPoint.getLat()).append(";");
            }else {
                if(i==0) {
                    coordinateArrayTemp=new double[2];
                    coordinateArrayTemp[0] = lat;
                    coordinateArrayTemp[1] = lon;
                }
                stringBuilderTrack.append(lat).append(",").append(lon).append(";");
            }
        }
        stringBuilderTrack.deleteCharAt(stringBuilderTrack.length() - 1);
        return stringBuilderTrack.toString();
    }

    public static GpsPoint gpsToGcJo2(double lat, double lon, Integer type) {
        if (COORDINATE_SYSTEM_BD0911.getId().equals(type)) {
            return GpsFormat.bd09ToGcj02(lat, lon);
        } else if (COORDINATE_SYSTEM_WGS84.getId().equals(type)) {
            return GpsFormat.wgs84ToGcj02(lat, lon);
        }
        return null;
    }

    public static Integer getCodeType(String gpsType) {
        try {
            if (StringUtils.isEmpty(gpsType)) {
                return null;
            }

            Integer index = getCodeByName(gpsType.toUpperCase());
            if (index == null) {
                index = Integer.valueOf(gpsType);
                if (!index.equals(GpsTypeEnums.COORDINATE_SYSTEM_BD0911.getId()) && !index.equals(GpsTypeEnums.COORDINATE_SYSTEM_GCJ02.getId()) && !index.equals(GpsTypeEnums.COORDINATE_SYSTEM_WGS84.getId())) {
                    return null;
                }
            }
            return index;
        } catch (Exception e) {
            return null;
        }
    }

    public static Integer getCodeByName(String type) {
        for (GpsTypeEnums failReason : values()) {
            if (failReason.getName().equals(type)) {
                return failReason.id;
            }
        }
        return null;
    }

    public static double[] pointToDouble(String point,String gpsType) {
        String[] arr = point.split("\\,");
        double x = Double.valueOf(arr[0]);
        double y = Double.valueOf(arr[1]);
        double[] floats = new double[2];
        floats[0] = x;
        floats[1] = y;
        return floats;
    }
}
